//
//  PlayManager.m
//  lisure0.1
//
//  Created by xalo on 16/4/21.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "PlayManager.h"

@implementation PlayManager

+(instancetype)shareManager{
    static PlayManager* share = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[PlayManager alloc] init];
    });
    return share;
}

//重写init方法去加载，当对象被出事话的时候被加载
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.player = [[AVPlayer alloc]init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(palyRadioEnd) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    }
    return self;
}

//播放电台
-(void)playRadio{

    [self.player play];
    [self startTimer];
}
//暂停电台
-(void)pauseRadio{

    [self.player pause];
    [self closeTimer];
}

//获取当前播放时间
-(NSInteger)fetchCurrentTime{

    CMTime time = self.player.currentTime;    
    //获取当前播放秒数
    if (time.timescale == 0) {
        return 0;
    }
    return time.value/time.timescale;
}

-(NSInteger)fetchTotalTime{

    CMTime time = self.player.currentItem.duration;
    //获取当前播放秒数
    if (time.timescale == 0) {
        return 0;
    }
    return time.value/time.timescale;
}

//获取播放进度
-(CGFloat)fetchPlayPragross{

    CGFloat currentTime = (CGFloat)[self fetchCurrentTime];
    CGFloat totalTime = (CGFloat)[self fetchTotalTime];
    if (totalTime == 0) {
        return 0;
    }
    return currentTime/totalTime;
}

//把时间转化为字符串
-(NSString*)changeTimeToString:(NSInteger)seconds{

    //获取分数
    NSInteger min = seconds/60;
    //获取秒数
    NSInteger sec = seconds%60;

    return [NSString stringWithFormat:@"%02ld:%02ld",min,sec];
}
-(void)preoareToPlayRadiowithUrl:(NSString*)url{

    if (!url) {

        return;
    }
    //根据url创建一个url对象
    AVPlayerItem* playItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:url]];
    [self.player replaceCurrentItemWithPlayerItem:playItem];

}

-(void)startTimer{

    if (self.timer) {
        return;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];

}

-(void)closeTimer{

    if (self.timer.isValid) {
       [self.timer invalidate];
    }
    self.timer = nil;
}

-(void)timeAction:(NSTimer*)sender{

    if (self.delegate&&[self.delegate respondsToSelector:@selector(playManagerFetchRestTime:progress:)]) {
        NSInteger restTime = [self fetchTotalTime] - [self fetchCurrentTime];
        NSString* restTimestr = [self changeTimeToString:restTime];
        CGFloat progress = [self fetchPlayPragross];
        [self.delegate playManagerFetchRestTime:restTimestr progress:progress];
    }
}


//滑块控制音乐的方法
-(void)bySliderValueToPlayRadio:(CGFloat)progress{

    [self pauseRadio];

    __weak PlayManager* weakSelf = self;
    [self.player seekToTime:CMTimeMake(progress*[self fetchTotalTime], 1) completionHandler:^(BOOL finished) {
        if (finished) {
            [weakSelf playRadio];
        }
    }];
    
}

-(void)palyRadioEnd{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(playManagerPlayRadioEnd)]) {
        [self.delegate playManagerPlayRadioEnd];
    }
}
@end
